package com.jinsp.webviewservice;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jinsp.webviewservice.webview.CustomWebView;
import com.jinsp.webviewservice.webview.IWebViewCallback;
import com.jinsp.webviewservice.webview.WebProgressView;
import com.jinsp.webviewservice.webview.WebViewConfig;

public final class WebViewFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener,
        IWebViewCallback {

    public static final String TAG = WebViewFragment.class.getCanonicalName();

    private WebViewConfig webViewConfig;
    private CustomWebView webView;
    private IFragment2ActivityCallback mFragment2ActivityCallback;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RelativeLayout webViewDisconnected;
    private ImageView networkDisconnectedImageView;
    private TextView networkDisconnectedTextView;
    private boolean isError;
    private WebProgressView webProgressView;

    public WebViewFragment() {
        // Required empty public constructor
    }

    public static WebViewFragment newInstance(WebViewConfig webViewConfig) {
        WebViewFragment fragment = new WebViewFragment();
        Bundle args = new Bundle();
        args.putParcelable(WebViewConfig.TAG, webViewConfig);
        fragment.setArguments(args);
        return fragment;
    }

    public void setFragment2ActivityCallback(IFragment2ActivityCallback fragment2ActivityCallback) {
        this.mFragment2ActivityCallback = fragment2ActivityCallback;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            webViewConfig = getArguments().getParcelable(WebViewConfig.TAG);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_web_view, container, false);
        if (view instanceof SwipeRefreshLayout) {
            swipeRefreshLayout = (SwipeRefreshLayout) view;
            swipeRefreshLayout.setOnRefreshListener(this);
            swipeRefreshLayout.setEnabled(webViewConfig.isCanRefresh());
            if (webViewConfig.getRefreshColor() != 0) {
                swipeRefreshLayout.setColorSchemeColors(webViewConfig.getRefreshColor());
            }
        }
        webProgressView = view.findViewById(R.id.web_view_progress);
        webProgressView.setProgressColor(webViewConfig.getRefreshColor());
        webViewDisconnected = view.findViewById(R.id.web_view_disconnected);
        networkDisconnectedImageView = webViewDisconnected.findViewById(R.id.iv_network_disconnected);
        networkDisconnectedTextView = webViewDisconnected.findViewById(R.id.tv_network_disconnected);
        webView = view.findViewById(R.id.web_view_fragment);
        String url = webViewConfig.getUrl();
        if (!TextUtils.isEmpty(url)) {
            webView.registerWebViewCallback(this, webViewConfig.getTitleName());
            webView.loadUrl(webViewConfig.getUrl());
        }
        return view;
    }

    public boolean back() {
        if (webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return false;
    }

    @Override
    public void onRefresh() {
        //重新加载页面
        webView.reload();
    }

    @Override
    public void updateTitle(String webTitle) {
        if (mFragment2ActivityCallback != null) {
            mFragment2ActivityCallback.updateTitleText(webTitle);
        }
    }

    @Override
    public void updateIcon(Bitmap webIcon) {

    }

    @Override
    public void startLoading(String url) {
        isError = false;
        Log.d(TAG, "开始加载网页: " + url);
//        webProgressView.setVisibility(View.VISIBLE);
    }

    @Override
    public void finishLoading(String url) {
        if (!isError) {
            Log.d(TAG, "网页加载完毕: " + url);
            if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
                swipeRefreshLayout.setRefreshing(false);
            }
            webView.setVisibility(View.VISIBLE);
            webViewDisconnected.setVisibility(View.GONE);
            webProgressView.stop();
            webProgressView.setVisibility(View.GONE);
        }
    }

    @Override
    public void loadError(int errorCode, String errorMessage) {
        isError = true;
        Log.e(TAG, "网页加载失败: " + errorMessage);
        if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        webProgressView.stop();
        webProgressView.setVisibility(View.GONE);
        webView.setVisibility(View.GONE);
        webViewDisconnected.setVisibility(View.VISIBLE);
        if (webViewConfig.getNetworkDisconnectedDrawable() != 0) {
            networkDisconnectedImageView.setImageDrawable(getResources().getDrawable(webViewConfig.getNetworkDisconnectedDrawable()));
        } else {
            networkDisconnectedImageView.setImageDrawable(getResources().getDrawable(R.drawable.network_disconnected));
        }
        if (TextUtils.isEmpty(webViewConfig.getNetworkDisconnectedTip())) {
            networkDisconnectedTextView.setText(getResources().getString(R.string.web_view_disconnected));
        } else {
            networkDisconnectedTextView.setText(webViewConfig.getNetworkDisconnectedTip());
        }
    }
}